package com.hzw.saas.common.pay.alipay.bean;

import com.alibaba.fastjson.JSON;
import com.alipay.api.internal.util.StringUtils;
import lombok.Getter;

/**
 * @author zzl
 * @since 2020-09-25
 */
@Getter
public class AliRefundReqContent extends AliTradeReqContent {

    /**
     * 原支付请求的商户订单号
     * 和支付宝交易号不能同时为空
     * 默认传入
     */
    private String out_trade_no;

    /**
     * 支付宝交易号
     * 和商户订单号不能同时为空
     * 如果同时传了 out_trade_no和 trade_no，则以 trade_no为准
     */
    private String trade_no;

    /**
     * 退款金额
     * 必选
     * 该金额不能大于订单金额,单位为元，支持两位小数
     */
    private String refund_amount;

    /**
     * 订单退款币种信息
     * 可选
     */
    private String refund_currency;

    /**
     * 退款的原因说明
     * 可选
     */
    private String refund_reason;

    /**
     * 标识一次退款请求，同一笔交易多次退款需要保证唯一
     * 如需部分退款，则此参数必传
     * 可选
     */
    private String out_request_no;

    /**
     * 商户的操作员编号
     * 可选
     */
    private String operator_id;

    // TODO: ZZL@2020-09-25 参数补全

    public static AliRefundReqContent build() {
        return new AliRefundReqContent();
    }

    public AliRefundReqContent out_trade_no(String outTradeNo) {
        this.out_trade_no = outTradeNo;
        return this;
    }

    public AliRefundReqContent trade_no(String tradeNo) {
        this.trade_no = tradeNo;
        return this;
    }

    public AliRefundReqContent refund_amount(String refundAmount) {
        if (!StringUtils.isNumeric(refundAmount)) {
            throw new IllegalArgumentException("refund_amount退款金额必须为数字");
        }
        if (refundAmount.contains(".")) {
            String[] split = refundAmount.split("\\.");
            if (split[1].length() > 2) {
                throw new IllegalArgumentException("refund_amount退款金额最多为2位小数");
            }
        }

        this.refund_amount = refundAmount;
        return this;
    }

    public AliRefundReqContent refund_currency(String refundCurrency) {
        this.refund_currency = refundCurrency;
        return this;
    }

    public AliRefundReqContent refund_reason(String refundReason) {
        this.refund_reason = refundReason;
        return this;
    }

    public AliRefundReqContent out_request_no(String outRequestNo) {
        this.out_request_no = outRequestNo;
        return this;
    }

    public AliRefundReqContent operator_id(String operatorId) {
        this.operator_id = operatorId;
        return this;
    }

    @Override
    public String buildBizContent() {
        if (StringUtils.isEmpty(this.out_trade_no)) {
            throw new IllegalArgumentException("out_trade_no商户订单号不能为空！");
        }

        if (StringUtils.isEmpty(this.refund_amount)) {
            throw new IllegalArgumentException("refund_amount退款金额不可为空！");
        }

        if (StringUtils.isEmpty(this.out_request_no)) {
            if (!StringUtils.isEmpty(this.out_trade_no)) {
                this.out_request_no = this.out_trade_no;
            } else if (!StringUtils.isEmpty(this.trade_no)) {
                this.out_request_no = this.trade_no;
            }
        }

        return JSON.toJSONString(this);
    }
}
